#include <iostream>
#include <memory>
using namespace std;
template <typename T>
class Vector
{
public:
    Vector()
        : _start(nullptr), _finish(nullptr), _end_of_storage(nullptr)
    {
    }
    ~Vector()
    {
        if (_start)
        {
            for (T *p = _start; p != _finish; ++p)
            {
                _alloc.destroy(p);
            }
            _alloc.deallocate(_start, _end_of_storage - _start);
        }
    }
    void push_back(const T &value)
    {
        if (_finish == _end_of_storage)
        {
            reallocate();
        }
        _alloc.construct(_finish++, value);
    }
    void pop_back()
    {
        --_finish;
        _alloc.destroy(_finish);
    }
    int size()
    {
        return _finish - _start;
    }
    int capacity()
    {
        return _end_of_storage - _start;
    }
    T &operator[](size_t index)
    {
        return _start[index];
    }

private:
    // 重新分配内存,动态扩容要用的
    void reallocate()
    {
        size_t new_capacity = capacity() ? capacity() * 2 : 1;
        T *new_start = _alloc.allocate(new_capacity);
        for (size_t i = 0; i < size(); ++i)
        {
            _alloc.construct(new_start + i, std::move(_start[i]));
            _alloc.destroy(_start + i);
        }
        _alloc.deallocate(_start, capacity());
        _finish = new_start + size();
        _end_of_storage = new_start + new_capacity;
        _start = new_start;
    }

private:
    static std::allocator<T> _alloc;
    T *_start;          // 指向数组中的第一个元素
    T *_finish;         // 指向最后一个实际元素之后的那个元素
    T *_end_of_storage; // 指向数组本身之后的位置
};
template <typename T>
allocator<T> Vector<T>::_alloc;
void test()
{
    Vector<int> vec;
    vec.push_back(1);
    vec.push_back(2);
    for (int i = 0; i < vec.size(); ++i)
    {
        cout << vec[i] << endl;
    }
    cout << vec.capacity()<< endl;
    cout << vec.size() << endl;

}
int main()
{
    test();
    return 0;
}
